<!-- 护理等级列表页 -->
<template>
  <div class="min-h serveGrade bg-wt">
    <!-- 组件整合，其他组件需要定义在这个div内部 -->
    <SearchFrom
    :searchData="pagination"
    @handleSearch="getList"
    @handleClear="handleClear"
    @handleReset="handleReset"
    ></SearchFrom>
    <DialogFrom
      ref="formRef"
      :visible="visible"
      :title="title"
      :planData="planFormData"
      :data="formDataRef"
      @handleClose="handleClose"
      @handleAdd="handleAdd"
      @handleEditForm="handleEditForm"
    ></DialogFrom>
    <TableList
      :listData="listData"
      :total="total"
      :pagination="pagination"
      @handleBulid="handleBulid"
      @getCurrent="getCurrent"
      @handleEdit="handleEdit"
      @handleClickDelete="handleClickDelete"
      @handleForbidden="handleForbidden"
    ></TableList>
    <deleteDialog
    :visible="deletevisible"
    @handleClose="deleteHandleClose"
    @handleDelete="handleDelete"
    ></deleteDialog>
  </div>
</template>

<script setup lang="ts">
import { Dialog, MessagePlugin } from 'tdesign-vue-next'
import TableList from './components/TableList.vue'
import DialogFrom from './components/DialogFrom.vue'
import SearchFrom from './components/SearchForm.vue'
import deleteDialog from '@/components/OperateDialog/index.vue'
import { onMounted, ref } from 'vue'
import { getLevelDetails, getLevelList, getnursAllPlanList, levelAdd, levelDelete, levelStatus, levelUpdate } from '@/api/serve'

const listData = ref([])
const total = ref(0)
const pagination = ref({
  pageNum: 1,
  pageSize: 5
})
const dataLoading = ref(true)

const visible = ref(false)
const title = ref('')

const planFormData = ref([])
const formDataRef = ref([])

const formRef = ref({})
//列表
const handleEdit = async (id) => {
  const result = await getLevelDetails(id);
  if(result.code == 200){
    formDataRef.value = result.data
    title.value='编辑'
    visible.value=true
  }else{
    MessagePlugin.error("编辑失败")
  }
}
const handleBulid = () => {
  visible.value = true
  title.value = '新增'
}

const handleAdd = async (formData)=>{
  const result = await levelAdd(formData);
  if(result.code == 200){
    MessagePlugin.success("新增成功")
    visible.value=false
    formRef.value.handleClear()
    getList()
  }else{
    MessagePlugin.error(result.msg)
  }
}
const handleEditForm = async (formData)=>{
  const result = await levelUpdate(formData)
  if(result.code == 200){
    MessagePlugin.success("修改成功")
    visible.value=false
    
    formRef.value.handleClear()
    getList()
  }else{
    MessagePlugin.error(result.msg)
  }
}
const handleClickDelete = async (id) => {
  typeId.value = id
  deletevisible.value = true
  
}
const handleDelete = async ()=>{
const result = await levelDelete(typeId.value);
  if(result.code == 200){
    deletevisible.value = false
    MessagePlugin.success("删除成功")
    getList()
  }else{
    MessagePlugin.error(result.msg)
  }
}
const handleForbidden = async (params) => {
  const result = await levelStatus(params);
  if(result.code == 200){
    MessagePlugin.success("修改成功")
    getList()
  }else{
    MessagePlugin.error(result.msg)
  }
}
const getCurrent = (pageInfo) => {
  console.log(1111)
  pagination.value.pageNum = pageInfo.current
  pagination.value.pageSize = pageInfo.pageSize
  getList()
}
const getList = async () => {
  const result = await getLevelList(pagination.value)
  if (result.code == 200) {
    listData.value = result.data.records
    total.value = result.data.total
  }
}

//查询护理计划
const getnursPlanList = async () => {
  const result = await getnursAllPlanList()
  if (result.code == 200) {
    planFormData.value = result.data
    console.log(123)
    console.log(planFormData)
  }
}
//关闭新增或修改对话框
const handleClose = () => {
  visible.value = false
}


const handleClear = (val)=>{
    if (val == 'name') {
    //清除name数据
    pagination.value.name = ''
  } else if (val == 'status') {
    //清除status数据
    delete pagination.value.status
  }
  //重新查询数据
  getList()
}

const handleReset = ()=>{
    //重置pagination
  pagination.value = {
    pageNum: 1,
    pageSize: 5
  }
  //重新查询数据
  getList()
}


onMounted(() => {
  getList()
  getnursPlanList()
})

//删除弹窗
const deletevisible = ref(false)
const deleteTitle = ref("删除")
const deleteText = ref("是否要删除?")
const typeId = ref('') // 设置删除id

const deleteHandleClose = ()=>{
  deletevisible.value = false;
}
</script>